﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class CreateUserWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();
        public User User;
        public CreateUserWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            btnCreateUser.Enabled =
                (txtEmailAddress.Text.Length > 0
                && txtPassword.Text.Length > 0
                && txtConfirmPassword.Text.Length > 0
                && txtPassword.Text == txtConfirmPassword.Text
                && txtFirstName.Text.Length > 0
                && txtLastName.Text.Length > 0);
        }

        private void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                int result = db.CreateUser(txtEmailAddress.Text.ToLower(), txtPassword.Text, txtFirstName.Text, txtLastName.Text, Constants.PASSWORD_SALT);

                User = db.Users.Single(u => u.EmailAddress == txtEmailAddress.Text);

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
